package org.jeecg.zhenxuan.Controller;

import org.jeecg.zhenxuan.CommonUtils.VelocityHelper;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.StringWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

//import com.xz.codegen.service.CodeEngine;

@Controller
@RequestMapping("/vm")
public class HelloVelocity {

/*	@Autowired
	CodeEngine ce;*/

	@RequestMapping("vmhello")
	public void hello(HttpServletRequest request, HttpServletResponse response , String template)
			throws Exception {
		// ..\..\templates\hello.vm
/*		String tempPath = URLDecoder.decode(request.getParameter("template"),
				"UTF-8");		*/

		String tempPath = URLDecoder.decode(template,
				"UTF-8");

		HashMap<String, Object> contextMap = new HashMap<String, Object>();
		contextMap.put("name", "Liang");
		contextMap.put("date", (new Date()).toString());
		// 为后面的展示，提前输入List数值
		List<String> temp = new ArrayList<String>();
		temp.add("1");
		temp.add("2");
		contextMap.put("list", temp);

		StringWriter writer = new StringWriter();

		Date ts = new Date();
		VelocityHelper.renderTemplate(tempPath, contextMap, writer);
		Date te = new Date();
		System.out.println("te-ts="+(te.getTime()-ts.getTime()));

		ts = new Date();
		File file = new File("C:\\temp\\hello.vm");
		String input = FileUtils.readFileToString(file, "UTF-8");
		writer = new StringWriter();
		VelocityHelper.renderString(input, contextMap, writer);
		te = new Date();
		System.out.println("te1-ts1="+(te.getTime()-ts.getTime()));

		response.getWriter().println(writer.toString());

	}

	@RequestMapping("vmstring")
	public void vmstring(HttpServletRequest request, HttpServletResponse response , String templateParam)
			throws Exception {
/*		String template = URLDecoder.decode(request.getParameter("template"),
				"UTF-8");		*/
		String template = URLDecoder.decode(templateParam,
				"UTF-8");
		HashMap<String, Object> contextMap = new HashMap<String, Object>();
		contextMap.put("name", "Liang");
		contextMap.put("date", (new Date()).toString());
		// 为后面的展示，提前输入List数值
		List<String> temp = new ArrayList<String>();
		temp.add("1");
		temp.add("2");
		contextMap.put("list", temp);

		StringWriter writer = new StringWriter();

		VelocityHelper.renderString(template, contextMap, writer);

		response.getWriter().println(writer.toString());

	}


	@RequestMapping("cehello")
	public void cehello(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

//		ce.test();

		response.getWriter().println("");
	}

	public static void main(String[] args) {
		try {
			HashMap<String, Object> contextMap = new HashMap<String, Object>();
			contextMap.put("name", "Liang");
			contextMap.put("date", (new Date()).toString());
			// 为后面的展示，提前输入List数值
			List<String> temp = new ArrayList<String>();
			temp.add("1");
			temp.add("2");
			contextMap.put("list", temp);
			StringWriter writer = new StringWriter();
			VelocityHelper.renderTemplate("hello.vm", contextMap, writer);
			System.out.println(writer.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
